<template>
  <div id="main">
    <h2>听课记录管理</h2>
    <div class="head">
      <el-button
        type="primary"
        style="margin:20px"
        @click="handleOpen('添加记录', 1)"
      >添加记录</el-button>
      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin:20px;"
      />

      <div id="table-area">
        <el-table
          v-loading="listLoading"
          :data="
            list.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          fit
          highlight-current-row
        >
          <el-table-column label="序号" type="index" align="center">
            <template scope="scope">
              <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="studentName"
            label="学生姓名"
            width="140"
          />
          <el-table-column
            align="center"
            prop="recordMainteacherName"
            label="主教老师"
            width="140"
          />
          <el-table-column
            align="center"
            prop="recordSuperviseteacherName"
            label="督导老师"
            width="140"
          />
          <el-table-column
            align="center"
            prop="recordLessonName"
            label="课程名称"
            width="140"
          />

          <el-table-column
            align="center"
            prop="recordClassroomName"
            label="教室"
            width="140"
          />

          <el-table-column
            align="center"
            prop="recordListentime"
            label="听课日期"
          />

          <el-table-column align="center" label="操作">

            <template slot-scope="Scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="handleOpen('修改记录', 2, Scope.row.recordId)"
              >编辑</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(Scope.row.recordId, Scope.row.studentName)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div id="pages">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[5, 10]"
            :page-size="pagesize"
            layout="total, sizes,prev, pager, next"
            :total="list.length"
            prev-text="上一页"
            next-text="下一页"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="records"
        :model="records"
        label-position="top"
        :rules="rules"
      >
        <el-form-item
          prop="studentName"
          label="学生姓名"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="records.recordStudentId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in student"
              :key="item.studentId"
              :label="item.String"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="recordMainteacherName"
          label="主教老师"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="records.recordMainteacherId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in mainteacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="recordSuperviseteacherName"
          label="督导老师"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="records.recordSuperviseteacherId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in teacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          prop="recordClassroomName"
          label="教室名称"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="records.recordClassroomId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in classroom"
              :key="item.roomId"
              :label="item.roomLoc"
              :value="item.roomId"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          prop="recordLessonName"
          label="课程名称"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="records.recordLessonId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in lesson"
              :key="item.lessonId"
              :label="item.lessonName"
              :value="item.lessonId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="recordListentime" label="听课时间">
          <div class="block">
            <el-date-picker
              v-model="records.recordListentime"
              type="datetime"
              placeholder="选择时间"
              format="yyyy -MM - dd  hh：mm :ss"
              value-format="yyyy-MM-dd hh:mm:ss"
              style="width:245px"
            />
          </div>
        </el-form-item>
        <el-form-item
          prop="recordTeachcontent"
          label="教学内容记录"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="records.recordTeachcontent"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="recordTeachadvantage"
          label="教学优点"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="records.recordTeachadvantage"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="recordTeachquestion"
          label="教学问题"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="records.recordTeachquestion"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="recordGuildsuggestion"
          label="指导建议"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="records.recordGuildsuggestion"
            autocomplete="off"
            maxlength="300"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('records')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('records')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  updateRecords,
  getAllRecords,
  getRecordsbyId,
  deleteRecords,
  addoneRecord,
  getTeacherString,
  getAllTeacherString,
  getAllstudentName,
  getAllClassRoom,
  getAlllesson
} from '@/api/supervision'
export default {
  data() {
    return {
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      searchTableInfo: '',
      records: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      student: {},
      mainteacher: {},
      teacher: {},
      classroom: {},
      lesson: {},
      search: '',
      formLabelWidth: '200px',
      rules: {
        studentName: [
          { required: true, message: '学生姓名不能为空', trigger: 'blur' }
        ],
        recordClassroomName: [{ required: true, message: '班级名称不能为空', trigger: 'blur' }],

        recordLessonName: [
          { required: true, message: '课程名称不能为空', trigger: 'blur' }
        ],
        recordListentime: [
          { required: true, message: '听课日期不能为空', trigger: 'blur' }
        ],
        recordMainteacherName: [
          { required: true, message: '主课教师姓名不能为空', trigger: 'blur' }
        ],
        recordSuperviseteacherName: [
          { required: true, message: '督导教师姓名不能为空', trigger: 'blur' }
        ],
        recordTeachadvantage: [
          { required: true, message: '教学优点不能为空', trigger: 'blur' }
        ],
        recordTeachcontent: [
          { required: true, message: '教学内容记录不能为空', trigger: 'blur' }
        ],
        recordTeachquestion: [
          { required: true, message: '教学问题不能为空', trigger: 'blur' }
        ],
        recordGuildsuggestion: [
          { required: true, message: '指导建议不能为空', trigger: 'blur' }
        ]

      },
      newRecords: {
        recordId: '',
        studentName: '',
        recordClassroomName: '',
        recordListentime: '',
        recordMainteacherName: '',
        recordSuperviseteacherName: '',
        recordLessonName: '',
        recordType: ''
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      console.log(this.searchTableInfo)
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getAllrecordstudent()
    this.getMainTeacher()
    this.getAllTeacher()
    this.getAllClassRooms()
    this.getAlllessons()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getAllrecordstudent() {
      getAllstudentName().then(res => {
        this.student = res.obj
      })
    },
    getMainTeacher() {
      getTeacherString(8).then(res => {
        this.mainteacher = res.obj
      })
    },
    getAllTeacher() {
      getAllTeacherString().then(res => {
        this.teacher = res.obj
      })
    },
    getAllClassRooms() {
      getAllClassRoom().then(res => {
        this.classroom = res.obj
      })
    },
    getAlllessons() {
      getAlllesson().then(res => {
        this.lesson = res.obj
      })
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllRecords().then(data => {
        this.newRecords
        for (var i = 0; i < data.obj.length; i++) {
          this.newRecords.recordId = data.obj[i].recordId
          this.newRecords.studentName = data.obj[i].studentName
          this.newRecords.recordClassroomName = data.obj[i].recordClassroomName
          this.newRecords.recordListentime = data.obj[i].recordListentime
          this.newRecords.recordMainteacherName =
            data.obj[i].recordMainteacherNam
          this.newRecords.recordLessonName = data.obj[i].recordLessonName
          this.newRecords.recordSuperviseteacherName =
            data.obj[i].recordSuperviseteacherName
          this.newRecords.recordType = data.obj[i].recordType
          this.tableList.push(this.newRecords)
          this.newRecords = {}
        }
        console.log(this.tableList)
        this.listLoading = false
      })
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该学生？姓名为' + username).then(() =>
        deleteRecords(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.records.recordListenTimeToString = this.records.recordListentime
          console.log(this.records)
          addoneRecord(this.records).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.records = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      console.log(id)
      this.records = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        getRecordsbyId(id).then(resp => {
          console.log(resp)
          this.records = resp.obj
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.records = {}
    },

    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.records.recordListenTimeToString = this.records.recordListentime
          console.log(this.records)
          updateRecords(this.records).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
